<?php
$pathArr = explode("/", $_SERVER["PHP_SELF"]);
$contextPath = $pathArr[1];
$rootPath = $_SERVER["DOCUMENT_ROOT"];
include_once $rootPath . "/" . $contextPath . "/apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$date = $_GET["date"];
$mode = $_GET["mode"];
if($mode == "update"){
	$getTimeTableData= $managerIUDS->queryData("*", "doctor_time_table", " WHERE doctor_time_table_id = ".$_GET["id"], "1", "=");
	$date = $getTimeTableData["date"];
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.: Doctor Time Table Detail :.</title>
<?php
require_once($rootPath . "/" . $contextPath . "/includes/css_inc.php");
require_once($rootPath . "/" . $contextPath . "/includes/js_inc.php");
?>
<script type="text/javascript">
var result = false;
			$(document).ready(function(){ 
				$("#save_data").click(function(){
					//alert("mode = " + $("#mode").val() + " date = "+$("#date").val() + " start_time = "+$("#start_time").val() + " end_time = "+$("#end_time").val());
					if(validate() && validateTime()) {
						$.ajax({
    		    					type: "GET",
									 url: "../actions/doctor_time_table_action.php",
    		    					data:{mode:$("#mode").val(),date:$("#date").val(),detail:$("#detail").val(),start_time:$("#start_time").val(),end_time:$("#end_time").val(),
											id : $("#doctor_time_table_id").val()
											},
									contentType: "application/json; charset=utf-8", 
									dataType: "json",
    		    					async: false,
    		    		            cache: false, 
    		    					success: function(data){
										//alert(data.start_time + " "+data.end_time);
										//alert(data.result);
										if(data.result == 0){
    		    							window.opener.location.href = "doctor_time_table.php";
    		    							window.close();
										}else{
											$("#start_time").val("");
											$("#end_time").val("");
											$("#start_time").focus();
											alert("ข้อมูลซ้ำกรุณากรอกข้อมูลใหม่");
										}
    		    					},
    		    					error: function(xhr,state,exception) { alert("exception = > " + exception); }
    		    				});//end ajax
						}
				});
				$("#delete_data").click(function(){
				if (confirm("คุณต้องการลบข้อมูลนี้ใช่หรือไม่?")) {
						$.ajax({
    		    					type: "GET",
									 url: "../actions/doctor_time_table_action.php",
    		    					data:{mode:"delete",id : $("#doctor_time_table_id").val()},
    		    					async: false,
    		    		            cache: false, 
    		    					success: function(data){
    		    						window.opener.location.href = "doctor_time_table.php";
    		    						window.close();
    		    					},
    		    					error: function(xhr,state,exception) { alert("exception = > " + exception); }
    		    				});//end ajax
						}
				});
			function validate(){
				var result = true;
				if($("#date").val() == "" || $("#date").val() == null){
					alert("กรุณากรอกวันที่");
					$("#date").focus(); 
					result= false;
				}else if($("#start_time").val() == "" || $("#start_time").val() == null){
					alert("กรุณากรอกเวลาเริ่มต้น");
					$("#start_time").focus(); 
					result= false;
				}else if($("#end_time").val() == "" || $("#end_time").val() == null){
					alert("กรุณากรอกเวลาสินสุด");
					$("#end_time").focus(); 
					result= false;
				}
				return result;
			}
			function validateTime(){
				var result = true;
				var startDate = parseInt($("#start_time").val());
				var endDate = parseInt($("#end_time").val());
				if(startDate > endDate){
					alert("เวลาไม่ถูกต้อง");
					$("#start_time").focus(); 
					result = false;
				}
				return result;
			}
			}); //end document
		</script>
</head>
<body>
<center>
<table border="0" width="75%">
	<thead class="ui-widget">
		<tr class="ui-widget-header">
			<th colspan="2" align="left">&nbsp;&nbsp;&nbsp;รายละเอียดตารางนัด</th>
		</tr>
	</thead>
	<tbody class="ui-widget-content">
		<tr>
			<td align="right">วันที่ :</td>
				<td><input type="text" name="date" id="date" value="<?=$date?>"></td>
			</td>
		</tr>
		<tr>
			<td align="right">รายละเอียด :</td>
			<td><textarea name="detail" id="detail" cols="21" rows="5"><?=$getTimeTableData["detail"]?></textarea></td>
		</tr>
		<tr>
			<td align="right">เวลาเริ่มต้น :</td>
				<td>
					<input type="text" name="start_time" id="start_time" onblur="return setTimeFormat(this);" onkeypress="return numbersOnly(event, false);"value="<?=$getTimeTableData["start_time"]?>" size="5">
					<span><font color="red">*กรอกเวลาในรูปแบบ HHMM</font></span>
				</td>
			</td>
		</tr>
		<tr>
			<td align="right">เวลาสิ้นสุด :</td>
				<td><input type="text" name="end_time" id="end_time" onblur="return setTimeFormat(this);" onkeypress="return numbersOnly(event, false);"value="<?=$getTimeTableData["end_time"]?>" size="5">
					<span><font color="red">*กรอกเวลาในรูปแบบ HHMM</font></span>
				</td>
			</td>
		</tr>
		<tr>
		<?php
		$colspan = 2;
		if($mode=="update"){
			$colspan = 3;
		}
		?>
			<td colspan="<?=$colspan?>" align="center">
			<input type="button" name="save_data" id="save_data" value="บันทึก">
			<input type="button" value="ยกเลิก"	onclick="window.close();">
		<?php
		if($mode=="update"){
		?>
			<input type="button" name="delete_data" id="delete_data" value="  ลบ  ">
		<?php
		}?>
		</td>
		</tr>
	</tbody>
</table>
<input type="hidden" id="mode" name="mode" value="<?=$mode?>">
<input	type="hidden" id="doctor_time_table_id" name="doctor_time_table_id" value="<?=$_GET["id"]?>">
</center>
</body>
</html>
